package oocl.eshop.ui.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oocl.eshop.po.Merchant;
import oocl.eshop.service.FoodManager;
import oocl.eshop.service.impl.FoodManagerImpl;

/**
 * Servlet implementation class DeleteFoodServlet
 */
public class DeleteFoodServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	FoodManager foodManager = new FoodManagerImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DeleteFoodServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		int fId = Integer.parseInt(request.getParameter("fId"));
		System.out.println(fId);
		int i = foodManager.deleteFood(fId);
		if (i != 1) {
			System.out.println("Food delete Error");
		}
		HttpSession session = request.getSession();
		Merchant merchant = (Merchant) session.getAttribute("merchant");
		if (merchant != null)
			session.setAttribute("foods", foodManager.findFoodBymid(merchant.getmId()));
		request.getRequestDispatcher("showFoodPage").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
